﻿using Mapster;
using Microsoft.Extensions.Configuration;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.SystemSettings.Dto;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.SystemSettings
{
    /// <summary>
    /// 流程催办配置接口服务
    /// </summary>
    public class FlowUrgingConfigurationAppService : BPMReportPlatformAppService, IFlowUrgingConfigurationAppService
    {
        private readonly SqlSugarClient _dbBusiness;

        public FlowUrgingConfigurationAppService(ISqlSugarAppService sqlSugarAppService, IConfiguration configuration)
        {
            _dbBusiness = sqlSugarAppService.GetBusinessInstance();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetFlowUrgingConfigurationOutput>> GetAllAsync(GetFlowUrgingConfigurationInput input)
        {
            RefAsync<int> totalCount = 0;

            var listViewDto = await _dbBusiness.Queryable<FlowUrgingConfiguration>()
                .WhereIF(!string.IsNullOrWhiteSpace(input.BTID), x => x.BTID.Contains(input.BTID))
                .WhereIF(!string.IsNullOrWhiteSpace(input.NodeName), x => x.NodeName.Contains(input.NodeName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.TimeSlot), x => x.TimeSlot.Contains(input.TimeSlot))
                .WhereIF(!string.IsNullOrWhiteSpace(input.FlowUrgingType), x => x.FlowUrgingType.Contains(input.FlowUrgingType))
                .OrderBy(x=>x.Id,OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize, totalCount);

            // var mapperList = ObjectMapper.Map<List<ApproverConfiguration>, List<GetApproveConfigurationOutput>>(listViewDto);
            var mapperList = listViewDto.Adapt<List<GetFlowUrgingConfigurationOutput>>();

            //返回结果
            return new PagedResultDto<GetFlowUrgingConfigurationOutput>()
            {
                TotalCount = totalCount,
                Items = mapperList
            };
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SaveAsync(SaveFlowUrgingConfigurationInput input)
        {
            var mapperInput = input.Adapt<FlowUrgingConfiguration>();

            if (input.Id != null)
            {
                await _dbBusiness.Updateable(mapperInput).ExecuteCommandAsync();
            }
            else
            {
                await _dbBusiness.Insertable(mapperInput).ExecuteCommandAsync();
            }
        }

        /// <summary>
        /// 删除审批配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            var cnt = await _dbBusiness.Deleteable<FlowUrgingConfiguration>().Where(x => x.Id.Equals(id)).ExecuteCommandAsync();
        }
    }
}
